package com.eb.bi.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

public class PluginParam {
	
	/// The parsed input params, list of key-value
	private HashMap<String,String> m_params = new HashMap<String,String>();
	/// The literal string of the input params
	private String m_literalParam = "";
	/// Used to indicate the parameters are resolved
	private boolean m_isParsed;
	/// Used to indicate the parameters are correctly resolved
	private boolean m_isValid;
	
	private String m_errorDesc;
	
	//��ѡ�����
	private List<String> m_checkedParams = new ArrayList<String>();
	
	public boolean parse(String[] args){
		
		/// Step 1, Parse and check input params
		if(!parseParams( args)){
			return false;
		}
		
		m_isParsed = true;
		
		/// Step 2, Check if required params exists. Premise: these parameters have been added to m_checkedParams
		if(!checkParams()){
			return false;
		}
		m_isValid = true;
		return m_isValid;
	}
	
	private boolean parseParams(String[] args){
		for(String arg : args){
			if(arg.contains("=")){
				if(!arg.substring(arg.length()-1,arg.length()).equals("=")){
					m_params.put(arg.split("=")[0], arg.split("=")[1]);
				}else{
					m_params.put(arg.split("=")[0],"");
				}
			}
			m_literalParam = m_literalParam + arg + " ";
		}
		return true;
	}
	
	private boolean checkParams(){
		for(int i = 0 ; i < m_checkedParams.size(); i++){
			String paramName = m_checkedParams.get(i);
			if(paramExists(paramName)){
				if(!getParam(paramName).trim().equals("")){
				}else{
					System.err.println("Parameter " +m_checkedParams.get(i)+" value is empty");
					this.m_errorDesc = "Parameter " +m_checkedParams.get(i)+" value is empty";
					return false;
				}
			}else{
				System.err.println("Parameter " +m_checkedParams.get(i)+" is missed");
				this.m_errorDesc = "Parameter " +m_checkedParams.get(i)+" is missed";
				return false;
			}	
		}
		return true;
	}
	
	public boolean paramExists(String paramName){
		return this.m_params.containsKey(paramName);
	}
	public String getParam(String paramName){
		return this.m_params.get(paramName);
	}
	public void addCheckedParam(String paramName){
		m_checkedParams.add(paramName);
	}
	

	
	public String getM_errorDesc() {
		return m_errorDesc;
	}

	public void setM_errorDesc(String desc) {
		m_errorDesc = desc;
	}
	
	public void print(){
		System.out.println("==========Input Param List==========" );
		System.out.println(m_params);
	}
	
	public static void main(String[] args){
		PluginParam PluginParam = new PluginParam();
		PluginParam.parse(args);
	}

	public HashMap<String, String> getM_params() {
		return m_params;
	}


}
